package Admin

/**
 * Created by Goland
 * User: wkk alisleepy@hotmail.com
 * Time: 2021/12/18 - 01:00
 * Desc: <登录相关操作>
 */

import (
	"ali-hr/Services/Admin"
	"ali-hr/Util"
	"github.com/gin-gonic/gin"
	"github.com/gookit/goutil/dump"
	"strings"
)

// Login 登录操作
func Login(c *gin.Context) {
	name := strings.Trim(c.PostForm("name"), " ")
	if Util.Debug() {
		dump.P("用户开始登录，用户名：", name)
	}
	if len(name) < 1 {
		Util.FailWithMessage("用户名不能为空", c)
		return
	}
	password := strings.Trim(c.PostForm("password"), " ")
	if len(password) < 1 {
		Util.FailWithMessage("密码不能为空", c)
		return
	}

	// 登录操作
	token, err := Admin.Login(name, password)
	// 登录失败
	if err != nil {
		if Util.Debug() {
			dump.P("登录失败，错误信息：", err)
		}
		Util.FailWithMessage("账户名密码错误，请重新登录", c)
	} else {
		// 登录成功
		Util.OkWithDetailed(gin.H{"token": token, "name": name}, "登录成功", c)
	}
}

// Logout 退出登录操作
func Logout(c *gin.Context) {

}
